<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-card>
      <el-form-item label="发起人" prop="createBy">
          <el-select v-model="queryParams.createBy" clearable filterable size="small" placeholder="请选择发起人" style="width:100%;">
            <el-option
              v-for="(item,index) in userList"
              :key="'bb'+index"
              :label="item.nickName"
              :value="item.userId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="跟进人" prop="followupId">
          <el-select v-model="queryParams.followupId" clearable filterable size="small" placeholder="请选择跟进人" style="width:100%;">
            <el-option
              v-for="(item,index) in userList"
              :key="'bb'+index"
              :label="item.nickName"
              :value="item.userId"
            />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="截止日期" prop="deadTime">
          <el-date-picker clearable size="small" style="width:100%;"
            v-model="queryParams.deadTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择截止日期">
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="事项类型" prop="exceed">
          <el-select v-model="queryParams.itemType" placeholder="请选择事项类型" clearable size="small">
            <el-option v-for="(dict,index) in dict.type.sys_item_type" :key="'d'+index" :label="dict.label" :value="parseInt(dict.value)" />
          </el-select>
        </el-form-item>
        <el-form-item label="跟进状态" prop="exceed">
          <el-select v-model="queryParams.status" placeholder="请选择跟进状态" clearable size="small">
            <el-option v-for="(dict,index) in dict.type.sys_item_status" :key="'d'+index" :label="dict.label" :value="parseInt(dict.value)" />
          </el-select>
        </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      </el-card>
    </el-form>

    <el-card style="margin-top:10px;">
      <el-table v-loading="loading" :data="itemsList" border>
        <el-table-column label="事项类型" align="center" prop="itemType" width="100">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_item_type" :value="scope.row.itemType"/>
          </template>
        </el-table-column>
        <el-table-column label="事项标题" align="left" prop="itemName" />
        <el-table-column label="事项关联" align="center" prop="itemParentName" />
        <el-table-column label="发起人" align="center" prop="createName" width="80" />
        <el-table-column label="发起日期" align="center" prop="createTime" width="120">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="当前跟进人" align="center" prop="followupName" width="120" />
        <el-table-column label="跟进状态" align="center" prop="status" width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.status==0" style="color:blue;">待跟进</span>
            <span v-if="scope.row.status==1" style="color:red;">跟进中</span>
            <span v-if="scope.row.status==2" style="color:green;">已结束</span>
          </template>
        </el-table-column>
        <el-table-column label="截止日期" align="center" prop="deadTime" width="120" />
        <el-table-column label="操作" align="center" width="300">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-document-copy"
              @click="handleView(scope.row)"
            >详情</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-s-promotion"
              @click="handleUpdate(scope.row)"
            >跟进</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改待办事项对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
      <el-card style="margin-top:-30px;">
        <div slot="header" class="clearfix">
          <span style="font-weight:bold;color:#1890FF;font-size:16px;">待办事项信息</span> 
        </div>
        <el-descriptions :column="1" border>
          <el-descriptions-item label="事项标题" :labelStyle="{width:'15%','text-align': 'center'}" :contentStyle="{ width:'85%',color: '#329dff' }">
            {{ itemDetail.itemName }}
          </el-descriptions-item>
          <el-descriptions-item label="事项描述" :labelStyle="{width:'15%','text-align': 'center'}" :contentStyle="{ width:'85%',color: '#329dff' }">
            {{ itemDetail.itemDesc }}
          </el-descriptions-item>
        </el-descriptions>
        <el-descriptions :column="2" border style="margin-top:-2px;">
          <el-descriptions-item label="事项类型" :labelStyle="{width:'15%','text-align': 'center'}" :contentStyle="{ width:'35%',color: '#329dff' }">
            <dict-tag :options="dict.type.sys_item_type" :value="itemDetail.itemType"/>
          </el-descriptions-item>
          <el-descriptions-item label="事项关联" :labelStyle="{width:'15%','text-align': 'center'}" :contentStyle="{ width:'35%',color: '#329dff' }">
            {{ itemDetail.itemParentName }}
          </el-descriptions-item>
          <el-descriptions-item label="发起人" :labelStyle="{width:'15%','text-align': 'center'}" :contentStyle="{ width:'35%',color: '#329dff' }">
            {{ itemDetail.createName }}
          </el-descriptions-item>
          <el-descriptions-item label="发起日期" :labelStyle="{width:'15%','text-align': 'center'}" :contentStyle="{ width:'35%',color: '#329dff' }">
            {{ parseTime(itemDetail.createTime, '{y}-{m}-{d}') }}
          </el-descriptions-item>
          <el-descriptions-item label="当前跟进人" :labelStyle="{width:'15%','text-align': 'center'}" :contentStyle="{ width:'35%',color: '#329dff' }">
            {{ itemDetail.followupName }}
          </el-descriptions-item>
          <el-descriptions-item label="事项进度" :labelStyle="{width:'15%','text-align': 'center'}" :contentStyle="{ width:'35%',color: '#329dff' }">
            <dict-tag :options="dict.type.sys_item_status" :value="itemDetail.status"/>
          </el-descriptions-item>
        </el-descriptions>
        <el-descriptions :column="1" border style="margin-top:-2px;">
          <el-descriptions-item label="事项附件" :labelStyle="{width:'15%','text-align': 'center'}" :contentStyle="{ width:'85%',color: '#329dff' }">
            <new-preview v-if="itemDetail.attach!=null" :src="itemDetail.attach" :width="150" :height="150" />
          </el-descriptions-item>
        </el-descriptions>
        <el-descriptions :column="1" border style="margin-top:-2px;">
          <el-descriptions-item label="结束描述" :labelStyle="{width:'15%','text-align': 'center'}" :contentStyle="{ width:'85%',color: '#329dff' }">
            {{ itemDetail.annotation }}
          </el-descriptions-item>
        </el-descriptions>
      </el-card>

      <el-card style="margin-top:10px;">
        <el-tabs v-model="activeName">
          <el-tab-pane label="跟进记录" name="first">
            <el-card style="margin-top:10px;" v-for="item in itemFormList" :key="item.uuid">
              <div slot="header" class="clearfix">
                <span style="font-weight:bold;color:#1890FF;font-size:16px;">{{ item.followupPerson }}</span> 
              </div>
              <el-descriptions :column="1" border>
                <el-descriptions-item label="跟进描述" :labelStyle="{width:'15%','text-align': 'center'}" :contentStyle="{ width:'85%',color: '#329dff' }">
                  {{ item.followupDesc }}
                </el-descriptions-item>
                <el-descriptions-item label="跟进附件" :labelStyle="{width:'15%','text-align': 'center'}" :contentStyle="{ width:'85%',color: '#329dff' }">
                  <new-preview v-if="item.followupAttach!=null" :src="item.followupAttach" :width="100" :height="100" />
                </el-descriptions-item>
                <el-descriptions-item label="跟进日期" :labelStyle="{width:'15%','text-align': 'center'}" :contentStyle="{ width:'85%',color: '#329dff' }">
                  {{ item.followupData }}
                </el-descriptions-item>
              </el-descriptions>
            </el-card>
          </el-tab-pane>
          <el-tab-pane label="追述记录" name="second">
            <el-timeline>
              <el-timeline-item v-for="item in commentsList" :key="item.uuid" :timestamp="item.createTime" placement="top" color="green">
                <el-card>
                  <div slot="header" class="clearfix">
                    <span style="font-weight:bold;color:#1890FF;font-size:16px;">{{ item.createName }}</span> 
                  </div>
                  <div style="margin-top:10px;line-height:30px;">
                    {{ item.comment }}
                  </div>
                </el-card>
              </el-timeline-item>
            </el-timeline>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-card style="margin-top:10px;" v-show="editFlag">
        <div slot="header" class="clearfix">
          <span style="font-weight:bold;color:#1890FF;font-size:16px;">跟进填写</span> 
        </div>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="跟进描述" prop="followupDesc">
            <el-input v-model="form.followupDesc" rows="5" type="textarea" placeholder="请输入跟进描述" />
          </el-form-item>
          <el-form-item label="跟进附件" prop="followupAttach">
            <image-upload-custom v-model="form.followupAttach" :limit="10000" :imageName="imageName"></image-upload-custom>
          </el-form-item>
          <el-row>
            <el-col :span="12">
              <el-form-item label="跟进日期" prop="followupData">
                <el-date-picker clearable size="small" style="width:100%;"
                  v-model="form.followupData"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="选择跟进日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="跟进人" prop="nextFollowupId">
                <el-select v-model="form.nextFollowupId" clearable filterable size="small" style="width:100%;">
                  <el-option
                    v-for="(item,index) in userList"
                    :key="'bb'+index"
                    :label="item.nickName"
                    :value="item.userId"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
      <div slot="footer" class="dialog-footer" v-show="editFlag">
        <el-button type="primary" @click="submitForm">提交跟进</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTodoItems, getItems, delItems, addItemsForm, listItemsForms,listItemComments } from "@/api/system/items";
import { listAllUser } from "@/api/system/user";
export default {
  name: "MyItems",
  dicts: ['sys_items_status','sys_exceed_state','sys_item_status','sys_item_type'],
  data() {
    return {
      imageName: "上传图片",
      // 遮罩层
      loading: true,
      activeName: "first",
      // 选中数组
      ids: [],
      itemDetail: {},
      // 非单个禁用
      single: true,
      editFlag: false,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 待办事项表格数据
      itemsList: [],
      itemFormList: [],
      exceedState: [],
      projectsList: [],
      commentsList: [],
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemName: null,
        itemDesc: null,
        followupId: null,
        followupName: null,
        createName: null,
        status: null,
        attach: null,
        deadTime: null,
        itemType: null,
        itemParentId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        followupDesc: [
          { required: true, message: "跟进描述不能为空", trigger: "blur" }
        ],
        followupData: [
          { required: true, message: "跟进日期不能为空", trigger: "blur" }
        ],
        nextFollowupId: [
          { required: true, message: "选择下一步跟进人", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getUserList();
  },
  methods: {
    getUserList() {
      listAllUser().then((response) => {
        this.userList = [];
        this.userList = response.data;
      });
    },
    /** 查询待办事项列表 */
    getList() {
      this.loading = true;
      listTodoItems(this.queryParams).then(response => {
        this.itemsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        itemId: null,
        followupId: null,
        uuid: null,
        followupDesc: null,
        followupAttach: null,
        followupData: null,
        nextFollowupId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新待办事项";
    },
    handleView(row){
      this.itemFormList = [];
      this.itemDetail = {};
      const uuid = row.uuid;
      this.activeName = "first";
      getItems(uuid).then(response => {
        this.open = true;
        this.title = "待办事项详情";
        this.itemDetail = response.data;
      });
      let query = {
        itemId:uuid
      }
      listItemsForms(query).then(res =>{
        this.itemFormList = res.data;
      })
      let query1 = {
        itemId:uuid
      }
      listItemComments(query1).then(res => {
        this.commentsList = res.data;
      });
      this.editFlag = false;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.itemFormList = [];
      this.itemDetail = {};
      this.reset();
      const uuid = row.uuid;
      this.activeName = "first";
      getItems(uuid).then(response => {
        this.form.itemId = row.uuid;
        this.form.followupId = row.followupId;
        this.open = true;
        this.title = "跟进待办事项";
        this.itemDetail = response.data;
      });
      let query = {
        itemId:uuid
      }
      listItemsForms(query).then(res =>{
        this.itemFormList = res.data;
      })
      let query1 = {
        itemId:uuid
      }
      listItemComments(query1).then(res => {
        this.commentsList = res.data;
      });
      this.editFlag = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addItemsForm(this.form).then(res => {
            this.$modal.msgSuccess("跟进成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const uuids = row.uuid || this.ids;
      this.$modal.confirm('是否确认删除待办事项 "' + row.itemName + '"的数据项？').then(function() {
        return delItems(uuids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/items/export', {
        ...this.queryParams
      }, `items_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
